package com.example.compose.jetsurvey

import android.util.Log
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.core.EaseIn
import androidx.compose.animation.core.EaseInOut
import androidx.compose.animation.core.EaseOut
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.togetherWith
import androidx.compose.animation.with
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.compose.jetsurvey.signinsignup.SignInRote
import com.example.compose.jetsurvey.signinsignup.SignUpRoute
import com.example.compose.jetsurvey.signinsignup.WelcomeRote
import com.example.compose.jetsurvey.survey.SurveyResultScreen
import com.example.compose.jetsurvey.survey.SurveyRoute

object Destinations {
    const val WELCOME_ROTE = "welcome"
    const val SIGN_UP_ROTE = "signup/{email}"
    const val SIGN_IN_ROTE = "signin/{email}"
    const val SURVEY_ROTE = "survey"
    const val SURVEY_RESULTS_ROTE = "survey_result"
}

@Composable
fun JetsurveyNavHost(navHostController: NavHostController = rememberNavController()) {

    NavHost(navController = navHostController, startDestination = Destinations.WELCOME_ROTE) {
        Log.d("TAG", "JetsurveyNavHost: ${navHostController.saveState()?.size()}")
        composable(route = Destinations.WELCOME_ROTE) {
            WelcomeRote(
                onNavigateToSignIn = {
                    navHostController.navigate(route = "signin/$it")
                },
                onNavigateToSignUp = {
                    navHostController.navigate(route = "signup/$it")
                },
                onSignInAsGuest = {
                    navHostController.navigate(Destinations.SURVEY_ROTE)
                }
            )
        }
        composable(
            route = Destinations.SIGN_IN_ROTE,
            enterTransition = {
                fadeIn(
                    animationSpec = tween(
                        300, easing = LinearEasing
                    )
                ) + slideIntoContainer(
                    animationSpec = tween(300, easing = EaseIn),
                    towards = AnimatedContentTransitionScope.SlideDirection.Start
                )
            },
            exitTransition = {
                fadeOut(
                    animationSpec = tween(
                        300, easing = LinearEasing
                    )
                ) + slideOutOfContainer(
                    animationSpec = tween(300, easing = EaseOut),
                    towards = AnimatedContentTransitionScope.SlideDirection.End
                )
            }
        ) {
            val email = it.arguments?.getString("email")
            SignInRote(
                email = email,
                onSignInSubmitted = { navHostController.navigate(Destinations.SURVEY_ROTE) },
                onSignInAsGuest = { navHostController.navigate(Destinations.SURVEY_RESULTS_ROTE) },
                onNavUp = navHostController::navigateUp
            )
        }

        composable(
            route = Destinations.SIGN_UP_ROTE,
            enterTransition = {
                slideIntoContainer(
                    AnimatedContentTransitionScope.SlideDirection.Left,
                    animationSpec = tween(300, easing = EaseInOut)
                )
            },
            exitTransition = {
                slideOutOfContainer(
                    AnimatedContentTransitionScope.SlideDirection.Right,
                    animationSpec = tween(300, easing = EaseInOut)
                )
            }
        ) {
            val startingEmail = it.arguments?.getString("email")
            SignUpRoute(
                email = startingEmail,
                onSignUpSubmitted = {},
                onSignInAsGuest = {},
                onNavUp = navHostController::navigateUp
            )
        }

        composable(route = Destinations.SURVEY_ROTE) {
            SurveyRoute(
                onSurveyComplete = {
                    navHostController.navigate(Destinations.SURVEY_RESULTS_ROTE)
                },
                onNavUp = navHostController::navigateUp
            )
        }

        composable(
            route = Destinations.SURVEY_RESULTS_ROTE,
            enterTransition = {
                slideInVertically(animationSpec = tween(durationMillis = 500))
            },
            exitTransition = {
                slideOutVertically(
                    animationSpec = tween(durationMillis = 500),
                    targetOffsetY = { fullHeight ->
                        fullHeight
                    }
                )
            }

        ) {
            SurveyResultScreen(
                onDonePressed = {
                    navHostController.popBackStack(
                        route = Destinations.WELCOME_ROTE,
                        inclusive = false
                    )
                }
            )
        }
    }
}
